package logic

import (
	"8-dshop/models"
	"context"

	"8-dshop/shoprpc/internal/svc"
	"8-dshop/shoprpc/shoprpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ProductListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewProductListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ProductListLogic {
	return &ProductListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ProductListLogic) ProductList(in *shoprpc.ProductListRequest) (*shoprpc.ProductListResponse, error) {
	// todo: add your logic here and delete this line
	var user models.User
	if err := user.FindUserById(models.DB, in.UserId); err != nil {
		return nil, err
	}
	var product models.Product
	findProducts, err := product.FindProduct(models.DB, in.UserId)
	if err != nil {
		return nil, err
	}
	var ProductLists []*shoprpc.ProductList
	for _, findProduct := range findProducts {
		productList := shoprpc.ProductList{
			Title:     findProduct.Title,
			Image:     findProduct.Image,
			Info:      findProduct.Info,
			Price:     float32(findProduct.Price),
			Stock:     findProduct.Stock,
			UserId:    findProduct.UserId,
			ProductId: int64(findProduct.ID),
		}
		ProductLists = append(ProductLists, &productList)
	}
	return &shoprpc.ProductListResponse{
		List: ProductLists,
	}, nil
}
